﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CDB.LinqModel.Data;
using CDB.LinqModel;
using CDB.LinqData;
using System.Collections.Generic;
using System.IO;
using System.Globalization;
using System.Drawing;
using System.Text.RegularExpressions;

public partial class back_AddBanner : System.Web.UI.Page
{
    #region Variable Declaration
    IDataRepository _dataObject = new DataRepository();
    Banner sBanner;
    #endregion

    #region Page Events
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["admin"] != null)
            {
                if (Request.QueryString["Id"] != null)
                {
                    fillContents(Request.QueryString["Id"].ToString());
                    //rfvPageName.Enabled = false;
                }
            }
            else
                Response.Redirect("login.aspx");
        }
    }
    #endregion

    #region Methods
    private void fillContents(string ContentId)
    {
        sBanner = new Banner(_dataObject);
        sBanner.Id = Convert.ToInt16(ContentId);
        sBanner.GetBannerById();

        if (sBanner.Id != 0)
        {
            txtBannerName.Text = sBanner.BannerName;
            ddlimgloction.SelectedValue = Convert.ToString(sBanner.ImageType);
            if (sBanner.ImageName != null)
            {
                hfimagename.Value = Convert.ToString(sBanner.ImageName);
                divimage.Visible = true;
                aviewimage.HRef = ConfigurationManager.AppSettings["BannerActualPath"].ToString() + sBanner.ImageName;
            }
            txtBannerLink.Text = Convert.ToString(sBanner.BannerLink);
            txtDescription.Text = sBanner.Description;
        }
    }
    private bool checkIfBannerExists(string bannerName)
    {
        Banner checkBanner = new Banner(_dataObject);
        checkBanner.BannerName = bannerName;
        checkBanner.GetBannerByBannerName();

        if (checkBanner.Id != 0)
            return true;
        else
            return false;
    }
    #endregion

    #region Button Events
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        sBanner = new Banner(_dataObject);
        sBanner.BannerName = txtBannerName.Text.ToString().Trim();
        //sBanner.ImagePath = ConfigurationManager.AppSettings["BannerActualPath"].ToString();
        sBanner.ImageType = Convert.ToInt32(ddlimgloction.SelectedValue);
        sBanner.BannerLink = Convert.ToString(txtBannerLink.Text.Trim());
        sBanner.Description = txtDescription.Text.ToString().Trim();
        if (Request.QueryString["Id"] != null)
        {
            sBanner.Id = Convert.ToInt32(Request.QueryString["Id"].ToString());

            if (uplImage.HasFile)
            {
                sBanner.ImageName = SaveImage(uplImage);
            }
            else
            {
                sBanner.ImageName = Convert.ToString(hfimagename.Value);
            }
            sBanner.Update();
            clearControls();
            divMessage.InnerHtml = "<strong>Well done!</strong>&nbsp;Banner updated successfully!";
            divMessage.Attributes.Add("class", "alert alert-success");
        }
        else
        {

            if (checkIfBannerExists(sBanner.BannerName))
            {
                divMessage.InnerHtml = "<strong>Oh snap!</strong>&nbsp;Banner with the same name already exists!";
                divMessage.Attributes.Add("class", "alert alert-error");
            }
            else
            {
                if (uplImage.HasFile)
                    sBanner.ImageName = SaveImage(uplImage);
                else
                    sBanner.ImageName = Convert.ToString(hfimagename.Value);
                
                sBanner.Create();
                clearControls();
                divMessage.InnerHtml = "<strong>Oh snap!</strong>&nbsp;Banner created successfully!";
                divMessage.Attributes.Add("class", "alert alert-success");
            }
        }
    }
    private void clearControls()
    {
        txtBannerLink.Text = string.Empty;
        txtBannerName.Text = string.Empty;
        txtDescription.Text = string.Empty;
    }
    private string SaveImage(FileUpload fuImage)
    {
        try
        {
            string filename = Path.GetFileName(fuImage.FileName);
            string[] fileArray = filename.Split('.');
            if (!Directory.Exists(Server.MapPath(ConfigurationManager.AppSettings["BannerActualPath"].ToString())))
            {
                Directory.CreateDirectory(Server.MapPath(ConfigurationManager.AppSettings["BannerActualPath"].ToString()));
            }

            fuImage.SaveAs(Server.MapPath(ConfigurationManager.AppSettings["BannerActualPath"].ToString() + filename));
            //string src = Server.MapPath(ConfigurationManager.AppSettings["BannerActualPath"].ToString() + filename);
            //if (!Directory.Exists(Server.MapPath(ConfigurationManager.AppSettings["BannerThumbPath"].ToString())))
            //{
            //    Directory.CreateDirectory(Server.MapPath(ConfigurationManager.AppSettings["BannerThumbPath"].ToString()));
            //}
            //string dest = Server.MapPath(ConfigurationManager.AppSettings["BannerThumbPath"].ToString());
            //if (ddlimgloction.SelectedValue == "1")
            //    CDBGeneral.CreateThumbnail(src, 185, 980, Path.GetExtension(fuImage.FileName), dest, filename);
            //else if (ddlimgloction.SelectedValue == "2")
            //    CDBGeneral.CreateThumbnail(src, 235, 170, Path.GetExtension(fuImage.FileName), dest, filename);
            //else if (ddlimgloction.SelectedValue == "3")
            //    CDBGeneral.CreateThumbnail(src, 130, 235, Path.GetExtension(fuImage.FileName), dest, filename);

            return (filename);
        }
        catch
        {
            return string.Empty;
        }
    }
    #endregion

}
